﻿using DataServer.Entities;
using DataServer.MySql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IsPay.Logic
{
    public class MenuSer
    {
        MenuDao menudao = new MenuDao();
        public IEnumerable<Menu> GetMenusByFather(String strUserid,String pcode)
        {
            IEnumerable<Menu> lsmenu = menudao.GetMenuByCode(strUserid, pcode);
            return lsmenu;
        }
        public Boolean CheckMenuDouble(String columnname, String strvalue)
        {
            Boolean isdouble = false;
            try
            {
                MenuDao md = new MenuDao();

                Menu act = md.GetMenuColumnName(columnname, strvalue);
                if (act == null || act.MenuId == null )
                {
                    isdouble = false;
                }
                else
                {
                    isdouble = true;
                }

            }
            catch (Exception e)
            {
                isdouble = false;
            }



            return isdouble;
        }

        public List<ShowMenu> GetShowMenu(IEnumerable<Menu> lsmenu)
        {
            List<ShowMenu> lsshowmenu = new List<ShowMenu>();
            if (lsmenu.Count() > 0)
            {
                Dictionary<String, ShowMenu> dicsm = new Dictionary<string, ShowMenu>();
                foreach(Menu mu in lsmenu)
                {
                    String mid = mu.MenuId;
                    ShowMenu smenu;
                    if (dicsm.ContainsKey(mu.MenuId))
                    {
                         smenu = dicsm[mu.MenuId];
                        smenu.RootMenu = mu;

                    }
                    else
                    {
                         smenu = new ShowMenu();
                        smenu.RootMenu = mu;
                        dicsm.Add(mu.MenuId, smenu);
                    }
                    
                    if (mu.MenuId.Length == 2)
                    {
                        lsshowmenu.Add(smenu);
                    }
                    else
                    {
                        String stfcode = mu.MenuId.Substring(0, mu.MenuId.Length - 2);
                        if (dicsm.ContainsKey(stfcode))
                        {
                            ShowMenu fshowmenu = dicsm[stfcode];
                            fshowmenu.LsSonMenu.Add(smenu);
                        }else
                        {
                            ShowMenu fshowmenu = new ShowMenu();
                            fshowmenu.LsSonMenu.Add(smenu);
                            dicsm.Add(stfcode,fshowmenu);
                        }
                    }
                }

            }

            return lsshowmenu;
        }

        public IEnumerable<Menu> QueryMenus(String strmenuid, String strmenuname)
        {
            IEnumerable<Menu> lsmenu = menudao.QueryMenus(strmenuid, strmenuname);
            return lsmenu;
        }
        
        public String UpdateMenus(List<DataServer.Entities.Menu> lsadd, List<DataServer.Entities.Menu> lsmodify, List<DataServer.Entities.Menu> lsdel,String struserid)
        {
            String strmsg = "OK";
            foreach(Menu mu in lsadd)
            {
                Boolean isdouble = CheckMenuDouble("Menuid", mu.MenuId);
                if (isdouble)
                {
                    strmsg = "MenuID:<"+mu.MenuId+"> 已经存在!";
                    return strmsg;
                }
            }
            MenuDao md = new MenuDao();
            if(!md.UpdateMenus(lsadd, lsmodify, lsdel,struserid))
            {
                strmsg = "更新失败!";
            }
            return strmsg;
        }
    }
}